<template>
	<div v-if="isAuth(['ROOT', 'DRIVER_LOCKDOWN:SELECT'])">
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
			<el-form-item prop="tel"><el-input v-model="dataForm.driverId" placeholder="司机编号" size="medium" clearable="clearable" /></el-form-item>
			<el-form-item prop="orderId"><el-input v-model="dataForm.orderId" placeholder="订单编号" size="medium" clearable="clearable" /></el-form-item>
			<el-form-item>
				<el-button size="medium" type="primary" @click="searchHandle()">查询</el-button>
				<el-button size="medium" type="primary" :disabled="!isAuth(['ROOT', 'DEPT:INSERT'])" @click="addHandle()">新增</el-button>
				<el-button size="medium" type="danger" :disabled="!isAuth(['ROOT', 'DEPT:DELETE'])" @click="deleteHandle()">批量删除</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" cell-style="padding: 4px 0" size="medium" style="width: 100%;">
			<el-table-column type="selection" :selectable="selectable" header-align="center" align="center" width="50" />
			<el-table-column type="index" header-align="center" align="center" width="100" label="序号">
				<template #default="scope">
					<span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="driverId" header-align="center" align="center" label="司机编号" min-width="200" />
			<el-table-column prop="orderId" header-align="center" align="center" label="订单编号" min-width="200" />
			<el-table-column prop="reason" header-align="center" align="center" label="具体原因" min-width="300" />
			<el-table-column prop="dateRange" header-align="center" align="center" label="日期范围" min-width="200" />
			<el-table-column prop="effect" header-align="center" align="center" label="状态" min-width="130" />
			<el-table-column header-align="center" align="center" width="150" label="操作">
				<template #default="scope">
					<el-button
						type="text"
						size="medium"
						:disabled="!isAuth(['ROOT', 'DRIVER_LOCKDOWN:UPDATE'])"
						@click="updateHandle(scope.row)"
					>
						修改
					</el-button>
					<el-popconfirm
					    width="220"
					    confirm-button-text="确认"
					    cancel-button-text="取消"
					    :icon="InfoFilled"
					    icon-color="#626AEF"
						@confirm="deleteHandle(scope.row.id)"
					    title="确认删除这条封禁记录?"
					  >
					    <template #reference>
					      <el-button type="text" size="medium" :disabled="!isAuth(['ROOT', 'DRIVER_LOCKDOWN:DELETE'])">
					      	删除
					      </el-button>
					    </template>
					  </el-popconfirm>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
			:current-page="pageIndex"
			:page-sizes="[10, 20, 50]"
			:page-size="pageSize"
			:total="totalCount"
			layout="total, sizes, prev, pager, next, jumper"
		></el-pagination>
		<add-or-update ref="addOrUpdate" @refreshDataList="loadDataList"></add-or-update>
	</div>
</template>

<script>
import AddOrUpdate from './driver_lockdown-add-or-update.vue';
export default {
	components: {
		AddOrUpdate
	},
	data: function() {
		return {
			dataForm: {
				driverId: null,
				orderId: null,
				effect: null
			},
			dataList: [],
			pageIndex: 1,
			pageSize: 10,
			totalCount: 0,
			dataListLoading: false,
			dataListSelections: [],
			dataRule: {
				driverId: [{ required: true, pattern: '^[1-9]\\d{17}$', message: '司机编号格式错误' }],
				orderId: [{ required: false, pattern: '^[1-9]\\d{17}$', message: '订单编号格式错误' }]
			}
		};
	},
	methods: {
		updateHandle(row){
			let data = JSON.parse(JSON.stringify(row));
			this.$refs.addOrUpdate.init(data);
			this.$refs.addOrUpdate.visible = true;
		},
		addHandle(){
			this.$refs.addOrUpdate.visible = true;
		},
		deleteHandle(id){
			let that = this;
			if(id) {
				//单个删除
				that.$http('driverLockDown/delete', 'POST', {"id":id}, true, function(resp) {
					if(resp.rows == 1){
						that.$message({
						    message: '删除成功!',
						    type: 'success'
						});
						that.searchHandle();
					}
				});
			} else {
				//批量删除
				console.log(that.dataListSelections);
			}
		},
		searchHandle(){
			let that = this;
			let data = {
				"page": that.pageIndex,
				"length": that.pageSize,
				"effect": that.dataForm.effect==""?null:that.dataForm.effect,
				"orderId": that.dataForm.orderId==""?null:that.dataForm.orderId,
				"driverId": that.dataForm.driverId==""?null:that.dataForm.driverId
			}
			that.$http('driverLockDown/list', 'POST', data, true, function(resp) {
				that.dataList = resp.result.list;
				let currentDate = new Date();
				for(let i=0;i<that.dataList.length;i++){
					that.dataList[i].dateRange = that.dataList[i].startDate + "——" + that.dataList[i].endDate;
					// 将 startDate 和 endDate 解析为日期对象
					let start = new Date(that.dataList[i].startDate);
					let end = new Date(that.dataList[i].endDate);
					// 判断当前日期是否在 startDate 和 endDate 范围内
					if (currentDate >= start && currentDate <= end) {
						that.dataList[i].effect = "有效"
					} else {
						that.dataList[i].effect = "过期"
					}
				}
			});
		},
		loadDataList(){
			this.searchHandle();
		}
		
	},
	created: function() {
		this.searchHandle();
	}
};
</script>

<style></style>
